﻿using System;
using System.Diagnostics;

namespace calculator
{
    /// <summary>
    /// Stores symbolic names (user) for the calculator
    /// </summary>
    class Store : IDisposable
    {
        public Store(int size, SymbolTable symtab)
        {
            _size = size;
            _cell = new double[size];
            _status = new Init[size];
            for (int i = 0; i < size; ++i)
                _status[i] = Init.NotInit;

            //add predefined const
            // TODO: revise if we need to add more
            Console.WriteLine("e = {0}", Math.Exp(1));
            int id = symtab.ForceAdd("e", 1);
            SetValue(id, Math.Exp(1));
            Console.WriteLine("pi = {0}", Math.Acos(0.0));
            id = symtab.ForceAdd("pi", 2);
            SetValue(id, 2.0 * Math.Acos(0.0));
        }
        public enum Init { NotInit, Init }
        public bool IsInit(int id)
        {
            return (id < _size && _status[id] != Init.NotInit);
        }
        public double Value(int id)
        {
            Debug.Assert(IsInit(id));
            return _cell[id];
        }
        public void SetValue(int id, double val)
        {
            if (id < _size)
            {
                _cell[id] = val;
                _status[id] = Init.Init;
            }
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    //disposed managed resource
                    _cell = null;
                    _status = null;
                }
                // release unmanaged resource
            }
            _disposed = true;

            // if available, call to base class
            // base.Dispose(disposing);
        }


        private bool _disposed = false;
        private int _size;
        private double[] _cell;
        private Init[] _status;
    }
}